#include "wrapping_integers.hh"

using namespace std;

Wrap32 Wrap32::wrap( uint64_t n, Wrap32 zero_point )
{
  // Your code here.
  return Wrap32 { Wrap32(n)+zero_point.raw_value_ };//取低32位
}

uint64_t Wrap32::unwrap( Wrap32 zero_point, uint64_t checkpoint ) const
{
  // Your code here.
  //找到与checkpoint最相近的数 可能在左也可能在右
  constexpr uint64_t upper=static_cast<uint64_t>(UINT32_MAX)+1;
  const uint32_t ckpt_mod=Wrap32::wrap(checkpoint,zero_point).raw_value_;//转换为32位
  uint32_t dif=raw_value_-ckpt_mod;
  if(dif<=(upper>>1)||checkpoint+dif<upper)//左边
    return checkpoint+dif;
  return checkpoint+dif-upper;//右边
}
